package com.xydtech.framework.web.service;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.domain.entity.SysDictData;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.system.service.ISysDeptService;
import com.xydtech.system.service.ISysDictDataService;
import com.xydtech.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RuoYi首创 html调用 thymeleaf 实现字典读取
 *
 * @author xydtech
 */
@Service("dict")
public class DictService {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 复选框返显处理
     * @Title: getChecked
     * @param checkedList
     * @param dictValue
     * @return boolean
     * @exception
     * @author JHON
     * @Date 2022/11/11 17:50
     **/
    public boolean getChecked(String checkedList, String dictValue) {
        String[] checkedArray = checkedList.split(",");
        for (int i = 0; i < checkedArray.length; i++) {
            if (checkedArray[i].equals(dictValue)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<SysDictData> getType(String dictType) {
        return dictTypeService.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型查询字典数据信息可自定义不包含
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<SysDictData> getType(String dictType, String excludeDictValues) {
        return dictDataService.getDictDataWithExcludes(dictType, excludeDictValues);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel(String dictType, String dictValue) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        return dictDataService.selectDictLabel(dictType, dictValue, sysDept.getDeptId());
    }
}
